package cn.aylog.springboot.controller;


import cn.aylog.common.entity.R;
import cn.aylog.servicebase.utils.MpUtils;
import cn.aylog.springboot.entity.Teacher;
import cn.aylog.springboot.entity.vo.TeacherQuery;
import cn.aylog.springboot.service.ITeacherService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author YongTay
 * @since 2022-01-15
 */
@Api("讲师管理")
@RestController
@RequestMapping("/teacher")
public class TeacherController {

    @Resource
    private ITeacherService teacherService;

    @ApiOperation("查询所有")
    @GetMapping("/all")
    public R findAll() {
        List<Teacher> list = teacherService.list();
        if(null == list) {
            return R.ok();
        }
        return R.ok().data("data", list);
    }

    @ApiOperation("带分页查询")
    @GetMapping("/page/{current}/{limit}")
    public R findOfPage(@PathVariable("current") Integer current, @PathVariable("limit") Integer limit) {
        IPage<Teacher> result = teacherService.page(new Page<>(current, limit), null);
        long total = result.getTotal();
        List<Teacher> list = result.getRecords();
        return R.ok().data("total", total).data("data", list);
    }

    @ApiOperation("带条件分页查询")
    @PostMapping("/pageCondition/{current}/{limit}")
    public R findOfPageCondition(
            @PathVariable("current") Integer current,
            @PathVariable("limit") Integer limit,
            @RequestBody(required = false) TeacherQuery teacherQuery
    ) {

        QueryWrapper queryMapper = MpUtils.createQueryMapper(teacherQuery);
        queryMapper.orderByDesc("gmt_create");
        IPage<Teacher> result = teacherService.page(new Page<>(current, limit), queryMapper);
        long total = result.getTotal();
        List<Teacher> list = result.getRecords();
        return R.ok().data("total", total).data("data", list);
    }

    @ApiOperation("根据讲师ID进行查询")
    @GetMapping("/findById/{id}")
    public R findById(@PathVariable("id") String id) {
        Teacher teacher = teacherService.getById(id);
        return R.ok().data("data", Arrays.asList(teacher));
    }


    @ApiOperation("根据ID删除")
    @DeleteMapping("/{id}")
    public R deleteById(@ApiParam(name = "id", value = "讲师ID", required = true) @PathVariable("id") String id) {
        boolean removeSuccess = teacherService.removeById(id);
        if(removeSuccess) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    @ApiOperation("添加讲师")
    @PostMapping("/add")
    public R addTeacher(@RequestBody Teacher teacher) {
        boolean saveSuccess = teacherService.save(teacher);
        if(saveSuccess) {
            return R.ok();
        }
        return R.error();
    }

    @ApiOperation("讲师信息更新")
    @PostMapping("/updateTeacher")
    public R updateTeacher(@RequestBody Teacher teacher) {
        boolean success = teacherService.updateById(teacher);
        if(success) {
            return R.ok();
        }
        return R.error();
    }

}
